package com.rzjm.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.*;
import com.rzjm.api.entity.*;
import com.rzjm.api.service.TerminalTemperatureService;
import com.rzjm.api.util.DateUtil;
import com.rzjm.api.util.ListUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class TerminalTemperatureServiceImpl extends BaseServiceImpl<TerminalTemperatureDao, TerminalTemperature> implements TerminalTemperatureService {

    @Resource
    private TerminalTemperatureDao temperatureDao;
    @Resource
    private DeviceNodeDao deviceNodeDao;

    @Override
    public List<TerminalTemperature> getListByDeviceId(Map<String, Object> map) {
        QueryWrapper<TerminalTemperature> qw = new QueryWrapper<>();
        List<TerminalTemperature> list = temperatureDao.selectList(qw);
        return list;
    }

    @Override
    public Map<String, Object> getChartData(Integer deviceId, String day, Integer nodeIndex,Integer insIndex) {
        System.out.println("getChartData nodeIndex:" + nodeIndex);
        System.out.println("getChartData insIndex:" + insIndex);
        double maxValue = 0d;
        Map<String,Object> mapReturn = new HashMap<>();

        List<String> times = new ArrayList<>();
        List<String> values = new ArrayList<>();

        for(int i = 0; i < 23; i++){
            QueryWrapper<TerminalTemperature> qw = new QueryWrapper<>();
            qw.eq("device_id",deviceId);

            qw.eq("node_index",nodeIndex);
            qw.eq("ins_index",insIndex);

            String hour = String.format("%02d", i);
            qw.between("create_time",day + " "+ hour +":00:00",day + " "+  hour +":59:59");
            qw.orderByAsc("wendu");
            qw.last("limit  1");

            List<TerminalTemperature> temperature = temperatureDao.selectList(qw);
            for (TerminalTemperature terminalTemperature : temperature) {
//                TerminalTemperature temperature = temperatureDao.selectOne(qw);

                times.add(DateUtil.getSimpleTime1(terminalTemperature.getCreateTime()));
                values.add(terminalTemperature.getWendu().toString());
                if (temperature != null) {
                } else {
//                times.add(hour +":00");
//                values.add("0.00");
                }
            }
        }

        QueryWrapper<DeviceNode> qw = new QueryWrapper<>();
        qw.eq("device_id",deviceId);
        qw.eq("node_index",nodeIndex);
        qw.eq("ins_index",insIndex);
        qw.last("limit 1");
        DeviceNode deviceNode = deviceNodeDao.selectOne(qw);
        if(deviceNode!=null){
            mapReturn.put("node_name",deviceNode.getTerminalName());
        }

        mapReturn.put("canvasImg","");
        mapReturn.put("times",times);
        mapReturn.put("values",values);
        mapReturn.put("count",times.size());
        mapReturn.put("nodeIndex",nodeIndex);
        mapReturn.put("insIndex",insIndex);
        mapReturn.put("maxValue",maxValue);
        mapReturn.put("minValue",maxValue);
        return mapReturn;
    }

    @Override
    public Map<String, Object> getChartData(Integer deviceId, String day,String beginHour,String endHour, Integer nodeIndex,Integer insIndex) {

        System.out.println("getChartData nodeIndex:" + nodeIndex);
        System.out.println("getChartData insIndex:" + insIndex);
        double maxValue = 0d;
        Map<String,Object> mapReturn = new HashMap<>();

        List<String> times = new ArrayList<>();
        List<String> values = new ArrayList<>();

        QueryWrapper<TerminalTemperature> qw = new QueryWrapper<>();
        qw.eq("device_id",deviceId);

        qw.eq("node_index",nodeIndex);
        qw.eq("ins_index",insIndex);

//        String bHour = String.format("%02d", beginHour);
//        String eHour = String.format("%02d", endHour);
        qw.between("create_time",day + " "+ beginHour +":00",day + " "+  endHour +":59");
        qw.orderByAsc("create_time");
        List<TerminalTemperature> temperatures = temperatureDao.selectList(qw);
        for (TerminalTemperature terminalTemperature : temperatures){
            times.add(DateUtil.getSimpleTime1(terminalTemperature.getCreateTime()));
            values.add(terminalTemperature.getWendu().toString());
        }

        QueryWrapper<DeviceNode> qwNode = new QueryWrapper<>();
        qwNode.eq("device_id",deviceId);
        qwNode.eq("node_index",nodeIndex);
        qwNode.eq("ins_index",insIndex);
        qwNode.last("limit 1");
        DeviceNode deviceNode = deviceNodeDao.selectOne(qwNode);
        if(deviceNode!=null){
            mapReturn.put("node_name",deviceNode.getTerminalName());
        }

        mapReturn.put("canvasImg","");
        mapReturn.put("times",times);
        mapReturn.put("values",values);
        mapReturn.put("count",times.size());
        mapReturn.put("nodeIndex",nodeIndex);
        mapReturn.put("insIndex",insIndex);
        mapReturn.put("maxValue",maxValue);
        mapReturn.put("minValue",maxValue);
        return mapReturn;
    }

    @Override
    public Map<String, Object> getChartDataByNode(Integer deviceId, String day,String beginHour,String endHour,DeviceNode node) {

        System.out.println("getChartData nodeIndex:" + node.getNodeIndex());
        System.out.println("getChartData insIndex:" + node.getInsIndex());
        double maxValue = 0d;
        Map<String,Object> mapReturn = new HashMap<>();

        List<String> times = new ArrayList<>();
        List<String> values = new ArrayList<>();

        QueryWrapper<TerminalTemperature> qw = new QueryWrapper<>();
        qw.eq("device_id",deviceId);
        qw.eq("node_index",node.getNodeIndex());
        qw.eq("ins_index",node.getInsIndex());
//        String bHour = String.format("%02d", beginHour);
//        String eHour = String.format("%02d", endHour);
        qw.between("create_time",day + " "+ beginHour +":00",day + " "+  endHour +":59");
        qw.orderByAsc("create_time");
        List<TerminalTemperature> temperatures = temperatureDao.selectList(qw);
        if(CollUtil.isNotEmpty(temperatures)){
            for (TerminalTemperature terminalTemperature : temperatures){
                times.add(DateUtil.getSimpleTime1(terminalTemperature.getCreateTime()));
                values.add(terminalTemperature.getWendu().toString());
            }
        }else{
            //如果在这个区间没有数据，查询最后的时间段，保证有数据
            QueryWrapper<TerminalTemperature> qwReset = new QueryWrapper<>();
            qwReset.eq("device_id",deviceId);
            qwReset.eq("node_index",node.getNodeIndex());
            qwReset.eq("ins_index",node.getInsIndex());
//            qwReset.between("create_time",day + " "+ beginHour +":00",day + " "+  endHour +":59");
            qwReset.orderByDesc("create_time");
            qwReset.last("limit 1");
            TerminalTemperature temperature = temperatureDao.selectOne(qwReset);
            if(temperature != null){

                Date lastRecord = temperature.getCreateTime();
                day = DateUtil.getDayStr(lastRecord);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(lastRecord);
                calendar.add(Calendar.HOUR_OF_DAY,-1);
                beginHour = calendar.get(Calendar.HOUR_OF_DAY) + ":00";

                Calendar calendarEnd = Calendar.getInstance();
                calendarEnd.setTime(lastRecord);
//                calendarEnd.add(Calendar.HOUR_OF_DAY,1);
                endHour = calendarEnd.get(Calendar.HOUR_OF_DAY) + ":59";

                QueryWrapper<TerminalTemperature> qwNew = new QueryWrapper<>();
                qwNew.eq("device_id",deviceId);
                qwNew.eq("node_index",node.getNodeIndex());
                qwNew.eq("ins_index",node.getInsIndex());
                qwNew.between("create_time",day + " "+ beginHour +":00",day + " "+  endHour +":59");
                qwNew.orderByAsc("create_time");
                temperatures = temperatureDao.selectList(qwNew);
                if(CollUtil.isNotEmpty(temperatures)){
                    for (TerminalTemperature terminalTemperature : temperatures){
                        times.add(DateUtil.getSimpleTime1(terminalTemperature.getCreateTime()));
                        values.add(terminalTemperature.getWendu().toString());
                    }
                }
            }
        }

        QueryWrapper<DeviceNode> qwNode = new QueryWrapper<>();
        qwNode.eq("device_id",deviceId);
        qwNode.eq("node_index",node.getNodeIndex());
        qwNode.eq("ins_index",node.getInsIndex());
        qwNode.last("limit 1");
        DeviceNode deviceNode = deviceNodeDao.selectOne(qwNode);
        if(deviceNode!=null){
            mapReturn.put("node_name",deviceNode.getTerminalName());
            //设备类型  2 - 测温度 3 - 测电流  4 - 测重量  5 - 雷达测距
            if(deviceNode.getDeviceType() == 2){
                mapReturn.put("data_title", "温度");
                mapReturn.put("data_unit", "℃");
            } else if(deviceNode.getDeviceType() == 3){
                mapReturn.put("data_title", "电流");
                mapReturn.put("data_unit", "A");
            }
        }

        mapReturn.put("canvasImg","");
        mapReturn.put("times",times);
        mapReturn.put("values",values);
        mapReturn.put("count",times.size());
        mapReturn.put("nodeIndex",node.getNodeIndex());
        mapReturn.put("insIndex",node.getInsIndex());
        mapReturn.put("maxValue",maxValue);
        mapReturn.put("minValue",maxValue);


        mapReturn.put("day",day);
        mapReturn.put("beginHour",beginHour);
        mapReturn.put("endHour",endHour);

        return mapReturn;
    }
}
